package com.qf.lanzhou.shop.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.qf.lanzhou.shop.pojo.entity.User;
import com.qf.lanzhou.shop.api.dao.LoginphoneMapper;
import com.qf.lanzhou.shop.api.service.LoginphoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author jian
 * @Date 2021/1/9 10:28
 * @JDK 1.8
 * @Version 1.0
 */
@Service
public class LoginphoneServiceimpl implements LoginphoneService {

    @Autowired
    private LoginphoneMapper loginphoneMapper;

    @Override
    public User selectphone(String phone) {
        User user = loginphoneMapper.selectphone(phone);
        return user;
    }


    @Override
    public boolean send(String phone, String templateCode, User user1) {
        //连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G4oEQcmMGEbo7axKe4h", "qDRhQp6uiDOj8WkWRyWNXvVXZ66D3h");
        //构建客户端,通过这个客户端连接
        IAcsClient client = new DefaultAcsClient(profile);

        //构建请求,我们发送请求给阿里,阿里再响应
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        //这个是发送短信的一个事件,也不用改
        request.setSysAction("SendSms");

        //自定义参数(手机号,验证码,签名,模板),通过模板嵌套验证码发送手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "少华有态度公众号");
        //模板,对应验证码(templateCode不能写死)
        request.putQueryParameter("TemplateCode", templateCode);

        //map是一个字符串,转为json格式
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(user1));


        try {
            //给客户端发送
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            //成功,返回出去
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        //不成功
        return false;
    }

    @Override
    public User selectmessage(String phone) {
        return loginphoneMapper.selectmessage(phone);
    }

    @Override
    public User selectlogintime(String phone2) {
        return loginphoneMapper.selectlogintime(phone2);
    }


    @Override
    public int update(String phone2) {
        return loginphoneMapper.update(phone2);
    }

    @Override
    public User selectallagain(String phone2) {
        return loginphoneMapper.selectallagain(phone2);
    }
}
